﻿// 位置相关
function GetAbsolutePositionLeft(e)
{ 
	var offset=e.offsetLeft; 
	if(e.offsetParent!=null) offset+=GetAbsolutePositionLeft(e.offsetParent); 
	return offset;
} 

function GetAbsolutePositionTop(e)
{ 
	var offset=e.offsetTop; 
	if(e.offsetParent!=null) offset+=GetAbsolutePositionTop(e.offsetParent); 
	return offset;
} 


// JavaScript Document

function AddFavorite(sURL, sTitle)
{
	try
	{
		window.external.addFavorite(sURL, sTitle);
	}
	catch (e)
	{
		try
		{
			window.sidebar.addPanel(sTitle, sURL, "");
		}
		catch (e)
		{
			alert("加入收藏失败。");
		}
	}
}


function SetHome(obj,url)
{
	if (document.all)
	{
		document.body.style.behavior='url(#default#homepage)';
		document.body.setHomePage(url);
	}
	else if (window.sidebar)
	{
		if(window.netscape)
		{
    		try
			{
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			}
			catch (e)
			{
				alert("操作被浏览器拒绝，请手动将 "+url+" 设为首页" );
			}
    	}
		else
		{
			try
			{
				var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
				prefs.setCharPref('browser.startup.homepage',url);
			}
			catch (e)
			{
				alert("操作被浏览器拒绝，请手动将 "+url+" 设为首页" );
			}
		}
	}
	else
	{
		alert("您的浏览器不支持设置首页，请手动将 "+url+" 设为首页" );
	}
}

function getNowYear()
{
	var year = new Date().getYear();
	if (year<1900) year+=1900;
	return year;
}

function getNowMonth()
{
	return new Date().getMonth()+1;
}

/**
 * 限制字符个数
 * @param className
 * @param len
 * @return
 */
function limitCharacterLength(className,len)
{
	$(className).each(
		function()
		{
			if(charLength($(this).text())>len)
			{
				$(this).text(charTruncate($(this).text(),0,len-3)+"...");
			}
		});
}

function charLength(string)
{
	return string.replace(/[^\u0000-\u00ff]/g,"aa").length;
}

function charTruncate(string,indexA,indexB)
{
	var str = "";
	for(var i=0;i<string.length;i++)
	{
		if (string.charAt(i)>="\u0000" && string.charAt(i)<="\u00ff")
			str += string.charAt(i);
		else
			str += "\u000d" + string.charAt(i);
	}
	
	return str.substring(indexA,indexB).replace(/\u000d/g,"");
}